from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
from django.contrib.auth.models import User

# 模型对象序列化器
class SnippetSerializer(serializers.ModelSerializer):
    '''
    使用ModelSerializer对象模型序列化器简化代码
    '''
    owner = serializers.ReadOnlyField(source='owner.username')
    class Meta:
        model = Snippet
        fields = ['id', 'title', 'code', 'linenos', 'language', 'style','owner']


class UserSerializer(serializers.ModelSerializer):
    '''
    用户序列化器
    '''
    snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())

    class Meta:
        # 使用 django.contrib.auth.models.User 模型
        model = User
        fields = ['id', 'username','snippets']

